package com.site.wisdom_construction.controller.publicModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.publicModule.ProfessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2021/12/29
 * @Description
 */
@Api(tags = "工种信息接口")
@RestController
@RequestMapping("Profession")
@CrossOrigin
public class ProfessionController {
    @Autowired
    private ProfessionService professionService;

    @ApiOperation(value = "增加工种信息", notes = "测试数据:")
    @PostMapping("/insertProfession")
    public JsonResult insertProfession(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = professionService.insertProfession(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入工种信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入工种信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除工种信息", notes = "测试数据:")
    @PostMapping("/deleteProfession")
    public JsonResult deleteProfession(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = professionService.deleteProfession(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除工种信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除工种信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改工种信息", notes = "测试数据:")
    @PostMapping("/updateProfession")
    public JsonResult updateProfession(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        int i = professionService.updateProfession(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改工种信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改工种信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询工种信息", notes = "测试数据:")
    @PostMapping("/findProfessionByPage")
    public Page findProfessionByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(professionService.Total(map));
        page.setItems(professionService.findProfessionByPage(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询工种信息", notes = "测试数据:")
    @PostMapping("/selectProfession")
    public OneDataResult selectProfession(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(professionService.selectProfession(map));
        return oneDataResult;
    }

    @ApiOperation(value = "批量导入工种信息", notes = "测试数据:")
    @PostMapping("/batchInsertProfession")
    public void batchInsertProfession(@RequestBody Map map){
        List list = (List) map.get("data");
        for (int i =0;i<list.size();i++){
            Map mapR = new HashMap();
            JSONObject json=(JSONObject) JSONObject.toJSON(list.get(i));
            mapR.put("professionName",json.get("name"));
            professionService.insertProfession(mapR);
        }
    }
}
